<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MAccessLogs extends MModel
{

    public static function createAccessLogs(
        $namespace,
        $start_time,
        $end_time
        )
    {
    	$user   = MUserManager::getInstance()->getCurrentUser();
    	$device = MUserManager::getInstance()->getCurrentDevice();
    	if (!isset($user->appId) || !isset($device->id)) {
    	    return ;
    	}
    	    	$db_manager = MDbManager::getInstance();
    	$sql = "select * from ".DB_PREFIX."_online_devices where application_id=".$user->appId." and user_id=".$user->id." and device_id=".$device->id;
    	$item = $db_manager->selectDb($sql);
    	if(count($item)>0){
    		$sql = "update ".DB_PREFIX."_online_devices set updated_at=now() where id=".$item[0]["id"];
    	}else{
    		$sql = "insert into ".DB_PREFIX."_online_devices(application_id,user_id,device_id,created_at,updated_at) values(".$user->appId.",".$user->id.",".$device->id.",now(),now())";
    	}
    	$db_manager->updateDb($sql);
    	    	$sql = "select count(*) as onlineMax,now() as currentTime from ".DB_PREFIX."_online_devices where (now()-updated_at)<=300";    	$status = $db_manager->selectDb($sql);
    	if(count($status)>0){
    		$change = true;
    		$onlineCount = $status[0]["onlineMax"];
    		$time        = $status[0]["currentTime"];
    		$sql  = "select option_value from ".DB_PREFIX."_options where option_name='max_online_users'";
    		$item = $db_manager->selectDb($sql);
    		if(count($item)>0){
    			$beforeCount = $item[0]['option_value'];
    			if($onlineCount<=intval($beforeCount)){
    				$change = false;
    			}
    		}
    		if($change){
    			    			if(count($item)>0){
    				$sql = "update ".DB_PREFIX."_options set option_value='".$onlineCount."',updated_at=now() where option_name='max_online_users'";
    			}else{
    				$sql = "insert into ".DB_PREFIX."_options(option_name,option_value,created_at,updated_at) values('max_online_users','$onlineCount',now(),now())";
    			}
    			$db_manager->updateDb($sql);
    		}
    	}
    	    	$currentDate = date("Y-m-d");
    	$hourperiod  = date("H");
    	    	                $use_time   = intval(($end_time - $start_time)*10000);		$sql  = "select * from ".DB_PREFIX."_pv where `current_date`='$currentDate' and application_id=".$user->appId." and hour_period=$hourperiod and namespace='$namespace'";
        $item = $db_manager->selectDb($sql);
    	if(count($item)>0){ 
    		$new_use_time  = ($item[0]['use_time']+$use_time)/2;
    		$sql = "update ".DB_PREFIX."_pv set use_count=use_count+1,use_time=".$new_use_time.",updated_at=now() where id=".$item[0]['id'];
    	}else{
    		$sql = "insert into ".DB_PREFIX."_pv(`current_date`, `application_id`, `hour_period`, `namespace`, `use_count`, `use_time`, `created_at`, `updated_at`) values('$currentDate',".$user->appId.",".$hourperiod.",'$namespace',1,$use_time,now(),now())";
    	}
    	$db_manager->updateDb($sql);
    	                
        return;
    }
}
?>